package cqupt.spark_log.utils;

import java.math.BigDecimal;
import java.util.Date;

import cqupt.spark_log.contrant.Contant;

public class ECUtil {
	public static double getEc(Date lastPoint, Date currentPoint, double cpu, double memorey) {
		double cpuUtilization = cpu/100;
		double memoreyUtilization = memorey/100;
		double cpuPower = Contant.CPU_1*cpuUtilization+Contant.CPU_2*Math.pow(cpuUtilization,2)+Contant.CPU_3*Math.pow(cpuUtilization,3);
		double menPower = Contant.MEM_1*memoreyUtilization+Contant.MEM_2*Math.pow(memoreyUtilization,2)+Contant.MEM_3*Math.pow(memoreyUtilization,3);
		double power = Contant.CON+cpuPower+menPower;
		long timeSpan = (currentPoint.getTime() - lastPoint.getTime())/1000;
		if (timeSpan<=0) {
			timeSpan = 1;
		}
		double energy = power*timeSpan;
		return get4point(energy);
	}
	//将double4舍五入到4位小数
	public static double get4point(double input){
		if (Double.isNaN(input)) {
			return 0;
		}
		BigDecimal b = new BigDecimal(input); 
		return b.setScale(4,BigDecimal.ROUND_HALF_UP).doubleValue();
	}
}
